package com.yswy.domain.common;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.yswy.domain.entity.*;
import com.yswy.domain.entity.tf.*;
import io.swagger.annotations.ApiModelProperty;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


//生成sql语句
public class SqlTableGenerator {

    public static String generateTableSql(Class<?> entityClass) {
        StringBuilder sqlBuilder = new StringBuilder();
        sqlBuilder.append("DROP TABLE IF EXISTS "+ getTableName(entityClass)+";");
        sqlBuilder.append("CREATE TABLE ");
        sqlBuilder.append(getTableName(entityClass));
        sqlBuilder.append(" (");

        List<String> columnDefinitions = getColumnDefinitions(entityClass);
        sqlBuilder.append(String.join(", ", columnDefinitions));

        sqlBuilder.append(");");

        return sqlBuilder.toString();
    }

    private static String getTableName(Class<?> entityClass) {
        // 生成表名，这里使用类名的下划线形式作为表名
        String className = entityClass.getSimpleName();
        return camelCaseToUnderscore(className);
    }

    private static List<String> getColumnDefinitions(Class<?> entityClass) {
        List<String> columnDefinitions = new ArrayList<>();

        Field[] fields = entityClass.getDeclaredFields();


        for (Field field : fields) {
            if (field.isAnnotationPresent(TableField.class)) {
                continue;
            }
            String columnName = camelCaseToUnderscore(field.getName());
            if (columnName.equals("serial_version_uid") || columnName.equals("log")){
                continue;
            }
            String columnType = getColumnType(field.getType());
            String columnDefinition = columnName + " " + columnType;

            // 判断是否为主键字段
            if (field.isAnnotationPresent(TableId.class)) {
                TableId tableId = field.getAnnotation(TableId.class);
                if (tableId.type() == IdType.AUTO) {
                    columnDefinition += " AUTO_INCREMENT";
                }
                columnDefinition += " PRIMARY KEY";
            }

            // 添加注释，可以根据需要在实体类的字段上使用 Swagger 的注解获取字段解释
            ApiModelProperty apiModelProperty = field.getAnnotation(ApiModelProperty.class);
            if (apiModelProperty != null) {
                String columnComment = apiModelProperty.value();
                columnDefinition += " COMMENT '" + columnComment + "'";
            }

            columnDefinitions.add(columnDefinition);
        }

        // 处理父类的字段
        Class<?> superClass = entityClass.getSuperclass();
        if (superClass != null && !superClass.equals(Object.class)) {
            List<String> parentColumnDefinitions = getColumnDefinitions(superClass);
            for (String s: parentColumnDefinitions) {
                if (s.contains("PRIMARY")){
                    columnDefinitions.add(0 , s);
                }else {
                    columnDefinitions.add(s);
                }
            }
        }

        return columnDefinitions;
    }

    private static String getColumnType(Class<?> fieldType) {
        // 根据字段类型返回对应的数据库列类型，这里只是一个示例，可以根据实际需要进行扩展
        if (fieldType == String.class) {
            return "VARCHAR(255)";
        } else if (fieldType == Integer.class || fieldType == int.class) {
            return "INT";
        } else if (fieldType == Long.class || fieldType == long.class) {
            return "BIGINT";
        } else if (fieldType == Boolean.class || fieldType == boolean.class) {
            return "BOOLEAN";
        } else if (fieldType == Date.class) {
            return "datetime";
        } else if (fieldType == BigDecimal.class) {
            return "DECIMAL(10,2)";
        } else if (fieldType == Boolean.class) {
            return "INT(1)";
        } else {
            // 其他类型的处理
            return "VARCHAR(255)";
        }
    }

    private static String camelCaseToUnderscore(String input) {
        // 将驼峰命名转换为下划线命名
        return input.replaceAll("([a-z])([A-Z]+)", "$1_$2").toLowerCase();
    }

    // 示例用法
    public static void main(String[] args) {
        String tableSql = generateTableSql(TgCallBackDelayed.class);
        System.out.println(tableSql);

    }



}

